package cn.edu.neusoft.yuejian_02.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import cn.edu.neusoft.yuejian_02.R;
import cn.edu.neusoft.yuejian_02.adapter.ClubAdapter;
import cn.edu.neusoft.yuejian_02.bean.ClubBean;
import cn.edu.neusoft.yuejian_02.model.ClubModel;
import es.dmoral.toasty.Toasty;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubListFragment extends Fragment implements View.OnClickListener {
    private View view=null;
    private RecyclerView cllist;
    private EditText et_sr;
    private TextView bt_sr;
    List<ClubBean> Club;
    ClubAdapter adapter;
   private String s;
    public ClubListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_club_list, container, false);

        initView();//初始化控件 (findViewById)

        initData();
        initEvent();
        return view;
    }

    private void initEvent() {
        bt_sr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //searchClub(s);
                s = et_sr.getText().toString();
    searchClub(s);
    //Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
}
});
        }

private void searchClub(String s) {
        Club.clear();
        cllist.removeAllViews();
        ClubModel clubModel = new ClubModel();
        Call<List<ClubBean>> call = clubModel.searchClub(s);
        call.enqueue(new Callback<List<ClubBean>>() {
@Override
public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {

        Club = response.body();
    if (!Club.isEmpty()){
        adapter = new ClubAdapter(getActivity(),Club);
        cllist.setAdapter(adapter);
    }else {
        Toasty.info(getActivity(),"没有搜索到结果").show();
    }


        }

@Override
public void onFailure(Call<List<ClubBean>> call, Throwable t) {

        }
        });
        }

private void initView() {

        cllist = (RecyclerView) view.findViewById(R.id.cllist);
        et_sr = (EditText) view.findViewById(R.id.et_sr);
        bt_sr = (TextView) view.findViewById(R.id.bt_sr);

        cllist.setLayoutManager(new LinearLayoutManager(this.getActivity()));


        }
private void initData() {

        ClubModel clubModel = new ClubModel();
        Call  call = clubModel.ClubList();
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            Club = (List<ClubBean>) response.body();
            adapter = new ClubAdapter(getActivity(),Club);
            cllist.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call call, Throwable t) {

        }
    });
        }



@Override
    public void onClick(View view) {

    }

  /*  @Override
    public void onSuccess(Object o) {
        List<ClubBean> Club = (List<ClubBean>) o;
        adapter = new ClubAdapter(getActivity(),Club);
        cllist.setAdapter(adapter);
    }

    @Override
    public void onFail() {

    }*/
}
